package org.linys.action.dict;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.linys.action.BaseAction;
import org.linys.model.dict.GoodsType;
import org.linys.model.system.Operator;
import org.linys.service.dict.GoodsTypeServ;
import org.linys.util.PageUtil;
import org.linys.util.ResponseMsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
@SuppressWarnings({  "unchecked" })
@Controller("GoodsTypeAction")
@Scope("prototype")
public class GoodsTypeAction extends BaseAction implements ModelDriven{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private GoodsType goodsType = new GoodsType();
	private ResponseMsgUtil responseMsgUtil;
	private PageUtil pageUtil;
	private GoodsTypeServ goodsTypeServ;
	
	/**
	 * 加载货物类型树的根节点
	 */
	public void selectTreeRoot() {
		try{
			GoodsType root = goodsTypeServ.selectTreeRoot();
			if(root.getChildrenCount()>0){
				List<GoodsType> goodsTypeList=goodsTypeServ.selectTreeNode(root);
				for(GoodsType goodsType:goodsTypeList){
					if(goodsType.getChildrenCount()>0){
						goodsType.setState("closed");
					}
				}
				root.setChildrenGoodsType(goodsTypeList);
			}
			this.responseMsgUtil.doResponse("["+root.toString()+"]");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 */
	public void selectTreeNode() {
		try{
			List<GoodsType> goodsTypeList=goodsTypeServ.selectTreeNode(goodsType);
			for(GoodsType goodsType:goodsTypeList){
				if(goodsType.getChildrenCount()>0){
					goodsType.setState("closed");
				}
			}
			this.responseMsgUtil.doResponse(goodsTypeList.toString());
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 单击树节点，属于该节点下的子节点信息
	 */
	public void getTreeNodeTabel() {
			
			
		try{
			
			
			this.pageUtil.cal(goodsTypeServ.count(goodsType), goodsType.getRows(), goodsType.getPage());

			List<GoodsType> goodsTypes = goodsTypeServ.getTreeNodeTabel(goodsType, this.pageUtil.getStart(), this.pageUtil.getRows());
			JSONArray jsonArray = JSONArray.fromObject(goodsTypes);
			JSONObject object = new JSONObject();
			object.put("total", this.pageUtil.getTotal());
			object.put("rows", jsonArray);
			this.responseMsgUtil.doResponse(object.toString());
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 添加地域城市
	 */
	public void add() {
			
			
		try{
			Operator operator=(Operator)request.getSession().getAttribute("operator_session");
			goodsType.setHandlerID(operator.getOperatorID_());
			goodsType.setOperatorID(operator.getOperatorID_());
			if (goodsTypeServ.save(goodsType) > 0) {
				JSONObject object = new JSONObject();
				object.put("success", 1);
				object.put("goodsTypeID", goodsType.getGoodsTypeID());
				this.responseMsgUtil.doResponse(object.toString());
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 批量删除
	 */
	public void mulDelete() {
		JSONObject object = new JSONObject();
		try{
			String [] goodsTypeIDArray=this.goodsType.getIds().split(",");
			for(String goodsTypeID:goodsTypeIDArray){
				goodsTypeServ.deleteByGoodsTypeID(new Integer(goodsTypeID));
			}
			
			object.put("success", 1);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		this.responseMsgUtil.doResponse(object.toString());
	}
	
	
	/**
	 * 修改地域城市
	 */
	public void update() {
			
			
		JSONObject object = new JSONObject();
		try{
			
			Operator currentOperator=(Operator)request.getSession().getAttribute("operator_session");
			goodsType.setOperatorID(currentOperator.getOperatorID_());
			if (goodsTypeServ.update(goodsType) > 0) {
				object.put("success", 1);
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}finally{
			this.responseMsgUtil.doResponse(object.toString());
		}
	}
	
	/**
	 * 选择货物类型
	 */
	public void choose() {
		try{
			
			this.pageUtil.cal(goodsTypeServ.countChoose(goodsType), goodsType.getRows(), goodsType.getPage());

			List<GoodsType> goodsTypes = goodsTypeServ.viewChoose(goodsType, this.pageUtil.getStart(), this.pageUtil.getRows());
			JSONArray jsonArray = JSONArray.fromObject(goodsTypes);
			JSONObject object = new JSONObject();
			object.put("total", this.pageUtil.getTotal());
			object.put("rows", jsonArray);
			this.responseMsgUtil.doResponse(object.toString());
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	public Object getModel() {
		return goodsType;
	}
	public ResponseMsgUtil getResponseMsgUtil() {
		return responseMsgUtil;
	}
	@Autowired
	public void setResponseMsgUtil(ResponseMsgUtil responseMsgUtil) {
		this.responseMsgUtil = responseMsgUtil;
	}
	public PageUtil getPageUtil() {
		return pageUtil;
	}
	@Autowired
	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}
	public GoodsTypeServ getGoodsTypeServ() {
		return goodsTypeServ;
	}
	@Autowired
	public void setGoodsTypeServ(GoodsTypeServ goodsTypeServ) {
		this.goodsTypeServ = goodsTypeServ;
	}
	
}
